package com.yuanduan.mplus.view;

import android.graphics.Color;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.cj.framework.app.BaseActivity;
import com.cj.framework.util.HttpImp;
import com.cj.framework.util.IHttp;
import com.cj.framework.util.RSAUtils;
import com.yuanduan.mplus.R;
import com.yuanduan.mplus.config.Config;
import com.yuanduan.mplus.model.DBManager;
import com.yuanduan.mplus.model.entity.MessageEntity;
import com.yuanduan.mplus.model.entity.User;
import com.yuanduan.mplus.presenter.MessageAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by cj on 2016/6/3.
 * Email:781806481@qq.com
 * 需修改
 */
public class MessageActivity extends BaseActivity implements View.OnClickListener {
    private Toolbar toolbar;
    private ListView listview;
    private EditText et_content;
    private TextView tv_send;

    private boolean flag=true;
    private IHttp iHttp;
    private MessageAdapter adapter;
    private User user;
    private List<MessageEntity> lists=new ArrayList<MessageEntity>();
    private IHttp.HttpInfoCallbackWithIndex callback=new IHttp.HttpInfoCallbackWithIndex() {
        @Override
        public void onSucceed(Object result, int index) {
            String result1= RSAUtils.decryptWithRSA((String) result);
            switch(index){
                case 0:
                    try {
                        JSONObject messages=new JSONObject(result1);
                        int rs=messages.getInt("RS");
                        if(rs==1){
                            JSONArray userid=messages.getJSONArray("UserID");
                            JSONArray isMe=messages.getJSONArray("isMe");
                            JSONArray Message=messages.getJSONArray("Message");
                            JSONArray CreationTime=messages.getJSONArray("CreationTime");
                            for(int i=0;i<userid.length();i++){
                                MessageEntity entity=new MessageEntity();
                                entity.setUserID(userid.getInt(i));
                                entity.setMe(isMe.getInt(i)==1?true:false);
                                entity.setMessage(Message.getString(i));
                                entity.setCreationTime(CreationTime.getString(i));
                                lists.add(entity);
                            }
                            adapter.notifyDataSetChanged();
                        }else{
                            Log.d("xx","rs error");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 1:
                    try {
                        JSONObject messages=new JSONObject(result1);
                        if(messages.getInt("RS")==1){
                            JSONArray userid=messages.getJSONArray("UserID");
                            JSONArray isMe=messages.getJSONArray("isMe");
                            JSONArray Message=messages.getJSONArray("Message");
                            JSONArray CreationTime=messages.getJSONArray("CreationTime");
                            for(int i=0;i<userid.length();i++){
                                if(isMe.getBoolean(i)!=true){
                                    MessageEntity entity=new MessageEntity();
                                    entity.setMe(false);
                                    entity.setUserID(userid.getInt(i));
                                    entity.setCreationTime(CreationTime.getString(i));
                                    entity.setMessage(Message.getString(i));
                                    lists.add(entity);
                                }
                            }
                            adapter.notifyDataSetChanged();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    String result2=RSAUtils.decryptWithRSA((String) result);
                    try {
                        JSONObject object=new JSONObject(result2);
                        if(object.getInt("RS")==1){

                        }else{

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }

        @Override
        public void onFailed(Object result, int index) {

        }
    };

    @Override
    protected void findId() {
        toolbar= (Toolbar) findViewById(R.id.toolbar);
        et_content= (EditText) findViewById(R.id.message_et_content);
        tv_send= (TextView) findViewById(R.id.message_tv_send);
        listview= (ListView) findViewById(R.id.message_listview);
    }

    @Override
    protected void logic() {
        user= DBManager.getInstance(getApplicationContext()).getUser();

        toolbar.setTitleTextColor(Color.BLACK);
        toolbar.setTitle("消息");
        toolbar.setNavigationIcon(R.mipmap.back_arrow);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                MessageActivity.this.finish();
            }
        });

        adapter=new MessageAdapter(this,lists);
        listview.setAdapter(adapter);
        iHttp=new HttpImp();
        tv_send.setOnClickListener(this);
        final Handler handler=new Handler();
        HashMap<String,String> hashMap=new HashMap<String, String>();
        hashMap.put("UserPhone",user.getUserPhone());
        hashMap.put("UserPassword",user.getUserPwd());
        String time=(System.currentTimeMillis()-1000*60*60*24-8*1000*60*600)+"";
        hashMap.put("timer",(System.currentTimeMillis()-1000*60*60*24)/1000+"");
        iHttp.httpInfoPostWithIndex(Config.URL+Config.REFRESHMESSAGES,Config.translateMap(hashMap),callback,0);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                HashMap<String,String> hashMap=new HashMap<String, String>();
                hashMap.put("UserPhone",user.getUserPhone());
                hashMap.put("UserPassword",user.getUserPwd());
                hashMap.put("timer",(System.currentTimeMillis()-1000)+"");
                iHttp.httpInfoPostWithIndex(Config.URL+Config.REFRESHMESSAGES,Config.translateMap(hashMap),callback,1);
               handler.postDelayed(this,60000);
            }
        },60000);
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_message;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.message_tv_send:
                String message=et_content.getText().toString().trim();
                et_content.setText("");
                MessageEntity entity=new MessageEntity();
                entity.setMessage(message);
                entity.setMe(true);
                entity.setCreationTime(System.currentTimeMillis()+"");
                entity.setUserID(user.getId());
                lists.add(entity);
                adapter.notifyDataSetChanged();
                HashMap<String,String> hashMap=new HashMap<>();
                hashMap.put("UserPhone",user.getUserPhone());
                hashMap.put("UserPassword",user.getUserPwd());
                hashMap.put("Message",message);
                iHttp.httpInfoPostWithIndex(Config.URL+Config.USERSENDMESSAGE,Config.translateMap(hashMap),callback,2);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        flag=false;
    }
}
